﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;

namespace QuickExcel
{
    public partial class createBookmark : Form
    {
        public createBookmark()
        {
            InitializeComponent();
            opWorkbook.Checked = true;
            lblFileName.Text = Globals.ThisAddIn.Application.ActiveWorkbook.Name;
            lblPathName.Text = Globals.ThisAddIn.Application.ActiveWorkbook.Path;
            lblSheetName.Text = Globals.ThisAddIn.Application.ActiveSheet.Name;
            lblRangeName.Text = Globals.ThisAddIn.Application.ActiveCell.Address;
        }

        private void createBookmark_Load(object sender, EventArgs e)
        {
            if (Globals.ThisAddIn.Application.ActiveWorkbook.Path == "")
            {
                System.Windows.Forms.MessageBox.Show("Please Save the Workbook first and try again");
                this.Close();
            }
        }

        private void opWorkbookSheetRange_CheckedChanged(object sender, EventArgs e)
        {
            if (opWorkbookSheetRange.Checked == true)
            {
                lblSheetName.Visible = true;
                lblRangeName.Visible = true;
                lblSheet.Visible = true;
                lblRange.Visible = true;
            }
        }

        private void opWorkbookSheet_CheckedChanged(object sender, EventArgs e)
        {
            if (opWorkbookSheet.Checked == true)
            {
                lblSheet.Visible = true;
                lblRange.Visible = false;
                lblSheetName.Visible = true;
                lblRangeName.Visible = false;
            }
        }

        private void opWorkbook_CheckedChanged(object sender, EventArgs e)
        {
            if(opWorkbook.Checked == true)
            {
                lblSheetName.Visible = false;
                lblRangeName.Visible = false;
                lblSheet.Visible = false;
                lblRange.Visible = false;
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (txtBookmarkName.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("The Bookmark Name could not left blank");
                txtBookmarkName.Focus();
            }
            else
            {
                String bookmark_detail="";
                
                if (existingBookmark(txtBookmarkName.Text))
                {
                    System.Windows.Forms.MessageBox.Show("Bookmark already exist. Please Enter Bookmark Name again");
                    txtBookmarkName.Text = "";
                    txtBookmarkName.Focus();
                }

                else
                {
                    FileStream fs = new FileStream("c:\\Program Files\\QuickExcel\\test.txt", FileMode.Append, FileAccess.Write);
                    StreamWriter tw = new StreamWriter(fs);
                
                    bookmark_detail = bookmark_detail + txtBookmarkName.Text + "-";
                    bookmark_detail = bookmark_detail + lblPathName.Text + "-";
                    bookmark_detail = bookmark_detail + lblFileName.Text + "-";
                    
                    if (lblSheetName.Visible == true)
                        bookmark_detail = bookmark_detail + lblSheetName.Text + "-";
                    
                    if (lblRangeName.Visible == true)
                        bookmark_detail = bookmark_detail + lblRangeName.Text + "-";
                    
                    tw.WriteLine(bookmark_detail);
                    
                    tw.Close();
                    
                    System.Windows.Forms.MessageBox.Show("Bookmark " + txtBookmarkName.Text + " Added");
                    
                    this.Close();
                }
            }
        }

        private bool existingBookmark(string p)
        {
            TextReader tw = new StreamReader("c:\\Program Files\\QuickExcel\\test.txt");
            String line;
            while ((line = tw.ReadLine()) != null)
            {
                String[] parts = line.Split('-');

                if (p == parts[0])
                {
                    tw.Close();
                    return true;
                }
            }
            tw.Close();
            return false;
        }
    }
}